package com.love.travel.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import com.love.common.core.constant.SecurityConstants;
import com.love.common.core.utils.DateUtils;
import com.love.common.core.utils.snow.SnowflakeIdGenerator;
import com.love.common.core.web.domain.AjaxResult;
import com.love.travel.domain.TravelAttractionTag;
import com.love.travel.mapper.TravelAttractionTagMapper;
import com.love.usedcar.api.RemoteCityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.love.travel.mapper.TravelAttractionMapper;
import com.love.travel.domain.TravelAttraction;
import com.love.travel.service.ITravelAttractionService;

/**
 * 旅游景点（含地区信息）Service业务层处理
 * 
 * @author love
 * @date 2025-10-15
 */
@Service
public class TravelAttractionServiceImpl implements ITravelAttractionService 
{
    @Autowired
    private TravelAttractionMapper travelAttractionMapper;
    @Autowired
    private TravelAttractionTagMapper travelAttractionTagMapper;
    @Autowired
    private RemoteCityService remoteCityService;
    //引用雪花算法
    private final SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(3, 3);

    /**
     * 查询旅游景点（含地区信息）
     * 
     * @param attractionId 旅游景点（含地区信息）主键
     * @return 旅游景点（含地区信息）
     */
    @Override
    public TravelAttraction selectTravelAttractionByAttractionId(Long attractionId)
    {
        return travelAttractionMapper.selectTravelAttractionByAttractionId(attractionId);
    }

    @Override
    public List<TravelAttraction> selectAttractionsByProductDestination(Long productId) {
        return travelAttractionMapper.selectAttractionsByProductDestination(productId);
    }

    @Override
    public List<TravelAttraction> selectAttractionsByItineraryId(Long itineraryId) {
        return travelAttractionMapper.selectAttractionsByItineraryId(itineraryId);
    }

    /**
     * 查询旅游景点（含地区信息）列表
     * 
     * @param travelAttraction 旅游景点（含地区信息）
     * @return 旅游景点（含地区信息）
     */
    @Override
    public List<TravelAttraction> selectTravelAttractionList(TravelAttraction travelAttraction)
    {
        if(travelAttraction.getRecommendScore()!=null && travelAttraction.getRecommendScore().compareTo(BigDecimal.ZERO)==0){
            travelAttraction.setRecommendScore(null);
        }
        return travelAttractionMapper.selectTravelAttractionList(travelAttraction);
    }

    /**
     * 新增旅游景点（含地区信息）
     * 
     * @param travelAttraction 旅游景点（含地区信息）
     * @return 结果
     */
    @Override
    public int insertTravelAttraction(TravelAttraction travelAttraction)
    {
        travelAttraction.setAttractionId(idGenerator.nextId());
        if(travelAttraction.getTagIds()!=null){
            TravelAttractionTag travelAttractionTag =  new TravelAttractionTag();
            travelAttractionTag.setAttractionId(travelAttraction.getAttractionId());
            travelAttractionTag.setCreateTime(DateUtils.getNowDate());
            for(Long tagId:travelAttraction.getTagIds()){
                travelAttractionTag.setTagId(tagId);
                travelAttractionTagMapper.insertTravelAttractionTag(travelAttractionTag);
            }
        }
        travelAttraction.setCreateTime(DateUtils.getNowDate());
        return travelAttractionMapper.insertTravelAttraction(travelAttraction);
    }

    /**
     * 修改旅游景点（含地区信息）
     * 
     * @param travelAttraction 旅游景点（含地区信息）
     * @return 结果
     */
    @Override
    public int updateTravelAttraction(TravelAttraction travelAttraction)
    {
        if(travelAttraction.getTagIds()!=null){
            travelAttractionTagMapper.deleteTravelAttractionTagByAttractionId(travelAttraction.getAttractionId());
            TravelAttractionTag travelAttractionTag =  new TravelAttractionTag();
            travelAttractionTag.setAttractionId(travelAttraction.getAttractionId());
            travelAttractionTag.setCreateTime(DateUtils.getNowDate());
            for(Long tagId:travelAttraction.getTagIds()){
                travelAttractionTag.setTagId(tagId);
                travelAttractionTagMapper.insertTravelAttractionTag(travelAttractionTag);
            }
        }
        travelAttraction.setUpdateTime(DateUtils.getNowDate());
        return travelAttractionMapper.updateTravelAttraction(travelAttraction);
    }

    /**
     * 批量删除旅游景点（含地区信息）
     * 
     * @param attractionIds 需要删除的旅游景点（含地区信息）主键
     * @return 结果
     */
    @Override
    public int deleteTravelAttractionByAttractionIds(Long[] attractionIds)
    {
        return travelAttractionMapper.deleteTravelAttractionByAttractionIds(attractionIds);
    }

    /**
     * 删除旅游景点（含地区信息）信息
     * 
     * @param attractionId 旅游景点（含地区信息）主键
     * @return 结果
     */
    @Override
    public int deleteTravelAttractionByAttractionId(Long attractionId)
    {
        return travelAttractionMapper.deleteTravelAttractionByAttractionId(attractionId);
    }

    /**
     * 远程调用二手车服务的省份列表
     * @return
     */
    @Override
    public AjaxResult selectProvinces() {
        return remoteCityService.selectProvinces(SecurityConstants.INNER);
    }

    @Override
    public AjaxResult selectCitys(String provinceCode) {
        return remoteCityService.selectCitys(SecurityConstants.INNER, provinceCode);
    }
}
